package org.grew.openEntity.service.Impl;

import org.grew.openEntity.repository.EditionRepository;
import org.grew.openEntity.repository.ProjectEditionRepository;
import org.grew.openEntity.po.logic.EditionObj;
import org.grew.openEntity.po.logic.ProjectEditionObj;
import org.grew.openEntity.service.EditionService;
import org.grew.openEntity.vo.EditionListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;

/**
 * @program: open_entity
 * @author: todd
 * @create: 2019-01-05 22:07
 * @description: ${description}
 */
@Service
@Transactional
public class EditionServiceImpl implements EditionService {


    @Autowired
    private EditionRepository editionRepository;

    @Autowired
    private ProjectEditionRepository projectEditionRepository;


    @Override
    public Integer createEdition(EditionObj edition, Long projectId) {
        int code = 0;
        if (edition.getContext().isEmpty() && edition.getEditionName().isEmpty() && projectId == null) {
            return 300;
        }
        List<EditionObj> editionObjs = editionRepository.checkEdition(edition.getEditionName());
        if (edition != null) {
            for (EditionObj editionObj : editionObjs) {
                ProjectEditionObj projectEditionObj = projectEditionRepository.checkProjectEdition(projectId, editionObj.getId());
                if (projectEditionObj != null) {
                    return 400;
                }
            }
        }
        try {
            EditionObj result = editionRepository.save(edition);
            projectEditionRepository.save(new ProjectEditionObj(projectId, result.getId()));
            return 200;
        } catch (Exception e) {
            return 500;
        }
    }

    @Override
    public void updateEdition(EditionObj edition, Long projectId) {
        EditionObj result = editionRepository.save(edition);
        projectEditionRepository.save(new ProjectEditionObj(projectId, result.getId()));
    }

    @Override
    public void deleteEdition(Long editionId) {
        editionRepository.deleteById(editionId);
    }

    @Override
    public EditionObj getEditionById(Long editionId) {
        Optional<EditionObj> byId = editionRepository.findById(editionId);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public List<EditionObj> getEditionByName(String editionName) {
        return editionRepository.findEditionObjByEditionName(editionName);
    }

    @Override
    public long getEditionCount() {
        return editionRepository.count();
    }

    @Override
    public EditionListVO getEditionList(Integer pageIndex, Integer pageSize) {
        EditionListVO editionList = new EditionListVO();
        List<Map<String,Object>> list = new ArrayList<>();
        Pageable pageable = new QPageRequest(pageIndex,pageSize);
        Page<EditionObj> page = editionRepository.findAll(pageable);
        editionList.setTotalPages(page.getTotalPages());
        List<EditionObj> editionObjs = page.getContent();
        for (EditionObj editionObj : editionObjs) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",editionObj.getId());
            map.put("editionName",editionObj.getEditionName());
            map.put("context",editionObj.getContext());
            String projectName = projectEditionRepository.getProjectByEditionId(editionObj.getId());
            map.put("projectName",projectName);
            map.put("state",editionObj.getState());
            list.add(map);
        }
        editionList.setList(list);
        return editionList;
    }

    @Override
    public List<Map<String,Object>> getEditionALL() {
        List<Map<String,Object>> result = new ArrayList<>();
        List<EditionObj> all = editionRepository.findAll();
        for (EditionObj editionObj : all) {
            Map<String,Object> map = new HashMap<>();
            map.put("editionId",editionObj.getId());
            map.put("editionName",projectEditionRepository.getProjectByEditionId(editionObj.getId()).concat("  "+editionObj.getEditionName()));
            result.add(map);
        }
        return result;
    }
}
